package com.itheima.controller;


import com.itheima.anno.Log;
import com.itheima.pojo.*;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j//记录日志
@RequestMapping("/students")
@RestController//@Controller+@ResponseBody
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result list(StudentQueryParam studentQueryParam){;
        log.info("查询条件:{}",studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable List<Integer> id){
        log.info("删除员工：{}", id);
        studentService.delete(id);
        return Result.success();
    }
    @Log
    @PostMapping//PostMapping 是 Spring MVC 注解，用于将 HTTP POST 请求映射到 add 方法，实现员工信息的添加功能。//从前端接受数据
    public Result add(@RequestBody Student student){
        log.info("新增员工：{}",student);
        studentService.save(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("查询员工：{}", id);
        Student student =  studentService.getInfo(id);
        return Result.success(student);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改员工：{}", student);
        studentService.update(student);
        return Result.success();
    }
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id,@PathVariable Integer score){
        log.info("修改员工：{}", id);
        studentService.updateViolation(id,score);
        return Result.success();
    }
}
